import { createApp, h, ref,createVNode } from 'vue';
import Popup from '@/components/popup.vue';
 
export function showModal(options) {
  const {
    title = '',
    message = '',
    showCancel = true,
    confirmText = '确定',
    cancelText = '取消',
    confirm,
    cancel,
  } = options;
 
  // 创建一个 Vue 应用实例
  const visible = ref(true);
  let resolveCallback, rejectCallback;
 
  const app = createApp({
    setup() {
      return { visible };
    },
    render() {
      return h(Popup, {
        visible: visible.value,
        title,
        message,
        showCancel,
        confirmText,
        cancelText,
        'onUpdate:visible': (newVisible) => {
          visible.value = newVisible;
          if (!newVisible) {
            // 用户关闭弹窗时销毁实例
            cleanup();
          }
        },
        onConfirm: () => {
          if (confirm) confirm();
          visible.value = false;
          resolveCallback?.(); // 触发 Promise 的 resolve
        },
        onCancel: () => {
          if (cancel) cancel();
          visible.value = false;
          rejectCallback?.(); // 触发 Promise 的 reject
        },
      });
    },
  });
 
  const container = document.createElement('div');
  document.body.appendChild(container);
 
  app.mount(container);
 
  const cleanup = () => {
    app.unmount();
    document.body.removeChild(container);
  };
 
 
  // 返回一个 Promise
  return new Promise((resolve, reject) => {
    resolveCallback = resolve;
    rejectCallback = reject;
  });
 
 
	
}

